#include "stdafx.h"
#include "HTTPResponserHeader.h"
#include "ServerResponseCode.h"
#include <map> 

std::map<size_t, std::string> serverDescription()
{
	static std::map<size_t, std::string> server;

	if (server.size() != 0)
	{
		return server;
	}

	server[SERVER_OK] = "200 OK";

	server[SERVER_NOCONTENT] = "204 No Content";

	server[SERVER_PARTIAL] = "206 Partial Content";

	server[SERVER_OBJMOVED] = "302 Moved Temporarily";

	server[SERVER_BADREQUEST] = "400 Bad Requestr";

	server[SERVER_NOTFOUND] = "404 Not Found";

	server[SERVER_BADMETHOD] = "405 Method Not Allowed";

	server[SERVER_SERVERERROR] = "500 Internal Server Error";

	server[SERVER_SERVERBUSY] = "500 Internal Server Error";

	server[SERVER_FORBIDDEN] = "500 Internal Server Error";

	server[SERVER_UNKNOWN] = "500 Internal Server Error";

	return server;
}

HTTPResponserHeader::HTTPResponserHeader(): responseCode(SERVER_UNKNOWN)
{
}


HTTPResponserHeader::~HTTPResponserHeader()
{
}

size_t HTTPResponserHeader::setResponseCode(size_t code)
{
	size_t temp = this->responseCode;

	this->responseCode = code;

	return temp;
}

size_t HTTPResponserHeader::addDefaultHeaderField()
{
	struct tm t;   //tm结构指针

	time_t timep = time(0);

	localtime_s(&t, &timep);

	char stTmp[100];

	// 转换为字符串并输出
	asctime_s(stTmp, &t);

	std::string a;
	for (int i = 11; i < 19; i++)
	{
		a += stTmp[i];
	}

	this->addHeader("Date", a);

	this->addHeader("Server", "WQ SERVER");

	return 2;
}

void HTTPResponserHeader::addHeader(const std::string &name, const std::string &value)
{
	std::list<std::pair<std::string, std::string>>::iterator headerIterator = this->find(name);

	if (headerIterator == this->httpHeader.end())
	{
		this->httpHeader.push_back(std::pair<std::string, std::string>(name, value));
	}
	else
	{
		headerIterator->second = value;
	}	
}

std::list<std::pair<std::string, std::string>>::iterator HTTPResponserHeader::find(const std::string &name)
{
	 std::list<std::pair<std::string, std::string>>::iterator iteraHeader = this->httpHeader.begin();

	 for (; iteraHeader != this->httpHeader.end(); iteraHeader++)
	 {
		 if (iteraHeader->first == name)
		 {
			 break;
		 }
	 }

	 return iteraHeader;
}

bool HTTPResponserHeader::formate()
{
	memFile.trunc();

	//写入第一行
	this->write(this->getFisrtLine());

	std::list<std::pair<std::string, std::string>>::iterator itera = this->httpHeader.begin();

	for (; itera != this->httpHeader.end(); itera ++)
	{
		this->write(itera->first.append(": ").append(itera->second).append("\r\n"));
	}
	// 写入一个空行表示结束
	this->write("\r\n");

	return false;
}

size_t HTTPResponserHeader::read(char * buffer, size_t length)
{
	return this->memFile.read(buffer, length);
}

void HTTPResponserHeader::reset()
{
	this->memFile.trunc();

	this->httpHeader.clear();

	this->responseCode = SERVER_UNKNOWN;
}

/*获取 相应头第一行*/
std::string HTTPResponserHeader::getFisrtLine()
{
	std::string str = "HTTP/1.1 ";

	str.append(serverDescription()[this->responseCode]);

	return str;
}

size_t HTTPResponserHeader::write(const std::string & str)
{
	return this->memFile.write(str.c_str(), str.length());
}
